from plantmanagement import db
from plantmanagement.models import plant
from plantmanagement.fun import Tools




class PlantController:

    def add(**self):
        u = plant(plantname=self['plantname'], introduce=self['introduce'], src=self['src'])
        Tools.db_commit(u)
        s = plant.query.filter_by(id=u.id).first()
        return {
            'code': 200,
            'data': s.to_json()
        }

    def dels(**self):
        u = plant.query.filter_by(id=self['plantid']).first()
        Tools.db_commit(u)
        return {
            'code': 200,
            'msg': '删除成功'
        }

    def edits(**self):
        u = plant.query.filter_by(id=self['plantid']).first()
        u.plantname = self['plantname'] if self['plantname'] else u.plantname
        u.introduce = self['introduce'] if self['introduce'] else u.introduce
        u.src = self['src'] if self['src'] else u.src
        Tools.db_commit(u)
        s = plant.query.filter_by(id=u.id).first()
        return {
            'code': 200,
            'data': s.to_json()
        }